package com.supreme.orderserver.feignapi;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.common.dto.GoodsToStorage;
import com.supreme.common.entityVO.CreateOrderVO;
import com.supreme.commonmybatis.entity.primary.MBGoods;
import com.supreme.commonmybatis.mapper.GoodsMapper;
import com.supreme.commonmybatis.mapper.OrderMapper;
import com.supreme.orderserver.service.OrderService;
import com.supreme.orderserver.service.StorageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/feign.goods")
public class GoodsApiCtrl {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private StorageService storageService;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderService orderService;

    @PostMapping(value = "create")
    public CreateOrderVO createStorage(@RequestBody GoodsToStorage bodyData) {
        final Long userId = bodyData.getUserId();
        final Long goodsId = bodyData.getGoodsId();
        final Integer goodsNum = bodyData.getGoodsNum();
        storageService.createStorage(goodsId, goodsNum);

        final CreateOrderVO createOrderVO = new CreateOrderVO();
        createOrderVO.setUserId(userId);
        createOrderVO.setGoodsId(goodsId);
        createOrderVO.setBuyNum(goodsNum);
        orderService.createOrder(createOrderVO);
        return createOrderVO;
    }

    @GetMapping(value = "goodsList")
    public List<MBGoods> goodsList() {
        return goodsMapper.selectList(Wrappers.emptyWrapper());
    }
}
